package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.QuestionnaireDetailDao;
import cn.topevery.garden.dal.logic.QuestionnaireContentLogic;
import cn.topevery.garden.entity.po.QuestionnaireDetail;
import cn.topevery.garden.entity.query.QuestionnaireDetailQuery;
import cn.topevery.garden.entity.vo.QuestionnaireDetailVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 调查题目
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class QuestionnaireDetailDaoImpl extends BaseDaoImpl<QuestionnaireDetail> implements QuestionnaireDetailDao {
    @Autowired
    private QuestionnaireContentLogic questionnaireContentLogic;
    @Override
    public <P extends DbCsp> List<QuestionnaireDetail> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<QuestionnaireDetailVO> listPage(QuestionnaireDetailQuery query) {
        String table = "t_questionnaire_detail tqd";

        String columns = "tqd.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tqd.c_id desc");
        sqlQueryMate
                .where(true, "tqd.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, QuestionnaireDetailVO.class);
    }

    @Override
    public QuestionnaireDetail getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void commitQuestionnaireDetail(QuestionnaireDetail detail, Integer userId) {
        SysUser user = UserRuntime.getSysUser();
        detail.setDbUpdateDate(LocalDateTime.now());
        detail.setDbUpdateId(userId);
        if (detail.getId() == null) {
            detail.setDbStatus(false);
            detail.setCreateId(user.userId);
            detail.setCreateTime(LocalDateTime.now());
            insert(user.userId, detail);
        } else {
            questionnaireContentLogic.deleteQuestionnaireDetailContentByDetailId(detail.getId(), userId);
            update(user.userId, detail);
        }
        questionnaireContentLogic.addQuestionnaireContent(detail.getContentList().stream()
                .peek(temp -> temp.setDetailId(detail.getId())).collect(Collectors.toList()), userId);
    }

    @Override
    public void deleteQuestionnaireDetailById(Integer id, Integer userId) {
        questionnaireContentLogic.deleteQuestionnaireDetailContentByDetailId(id, userId);
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_questionnaire_detail");
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(true, "c_db_update_id", userId)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now())
                .where(true, "c_id", ScriptConditionEnum.Eq, id);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }

    @Override
    public List<QuestionnaireDetail> getQuestionnaireDetailList(Integer id) {
        SqlQueryMate sqlQueryMate = new SqlQueryMate("t_questionnaire_detail").setColumns("*");
        sqlQueryMate.where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(id!=null, "c_main_id", ScriptConditionEnum.Eq, id);
        List<QuestionnaireDetail> detailList = selectList(sqlQueryMate, QuestionnaireDetail.class);
        detailList = detailList.stream().peek(temp ->
                temp.setContentList(questionnaireContentLogic.getContentListByDetailId(temp.getId()))).collect(Collectors.toList());
        return detailList;
    }

    @Override
    public QuestionnaireDetail getQuestionnaireDetail(Integer id) {
        QuestionnaireDetail detail = getEntity(id);
        detail.setContentList(questionnaireContentLogic.getContentListByDetailId(id));
        return detail;
    }
}
